package stone.ast;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import runner.Environment;
import stone.StoneException;

import java.util.Iterator;
import java.util.List;

public class ASTList extends ASTree {
  protected List<ASTree> children;

  public ASTList(List<ASTree> list) {
    children = list;
  }

  @Override
  public ASTree child(int i) {
    return children.get(i);
  }

  @Override
  public int numChildren() {
    return children.size();
  }

  @Override
  public Iterator<ASTree> children() {
    return children.iterator();
  }

  @Override
  public String toString() {
    var builder = new StringBuilder();
    builder.append('(');
    var sep = "";
    for (var t : children) {
      builder.append(sep);
      sep = " ";
      builder.append(t.toString());
    }
    return builder.append(')').toString();
  }

  @Override
  public String location() {
    for (var t : children) {
      var s = t.location();
      if (s != null) {
        return s;
      }
    }
    return null;
  }

  @Override
  public Iterator<ASTree> iterator() {
    return null;
  }

  @NotNull
  @Override
  public Object eval(@Nullable Environment env) {
    throw new StoneException("cannot eval: " + toString(), this);
  }
}
